package cn.lili.controller.grab;

import cn.lili.common.thread.ThreadConfig;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.grab.GrabGoodsParam;
import cn.lili.modules.grab.GrabNoticeParam;
import cn.lili.modules.grab.GrabOrderParam;
import cn.lili.modules.grab.dingdong.base.BaseResult;
import cn.lili.modules.grab.dingdong.config.DingdongConfig;
import cn.lili.modules.grab.dingdong.res.notice.GoodsCallbackRequest;
import cn.lili.modules.grab.dingdong.res.notice.OrderCallbackLockRequest;
import cn.lili.modules.grab.dingdong.res.notice.OrderCallbackRequest;
import cn.lili.modules.grab.dingdong.utils.AESUtil;
import cn.lili.modules.grab.dingdong.utils.RSAUtil;
import cn.lili.modules.grab.enums.SourceEnums;
import cn.lili.modules.grab.factory.goods.GrabGoodsFactory;
import cn.lili.modules.grab.factory.notice.GrabNoticeFactory;
import cn.lili.modules.grab.factory.notice.GrabNoticeService;
import cn.lili.modules.grab.factory.order.GrabOrderFactory;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@RestController
@Api(tags = "商品采集客户端")
@RequiredArgsConstructor
@RequestMapping("/goods/grab/dingdong/client")
public class GrabClientController {

    @Resource
    private GrabGoodsFactory grabGoodsFactory;
    @Resource
    private GrabOrderFactory grabOrderFactory;
    @Resource
    private GrabNoticeFactory grabNoticeFactory;
    @Resource
    private DingdongConfig dingdongConfig;
    @Resource
    private ThreadConfig threadConfig;

    @ApiOperation(value = "采集所有商品分类")
    @PostMapping(value = "/grabGoodsCate")
    public ResultMessage<Object> grabGoodsCate(@RequestBody GrabGoodsParam grabParam) {
        return grabGoodsFactory.generateGoodsCateFactory(grabParam);
    }

    @ApiOperation(value = "采集所有商品")
    @PostMapping(value = "/grabGoods")
    public ResultMessage<Object> grabGoods(@RequestBody GrabGoodsParam grabParam) {
        Objects.requireNonNull(threadConfig.getAsyncExecutor()).execute(() -> {
            grabGoodsFactory.generateGoodsFactory(grabParam);
        });
        return null;
    }

    @ApiOperation(value = "获取配送方式")
    @PostMapping(value = "/getYfMode")
    public ResultMessage<Object> getYfMode(@RequestBody GrabGoodsParam grabParam) {
        return grabGoodsFactory.generateYfModeFactory(grabParam);
    }

    @ApiOperation(value = "运费计算")
    @PostMapping(value = "/freightCalc")
    public ResultMessage<Object> freightCalc(@RequestBody GrabOrderParam grabParam) {
        return grabOrderFactory.generateFreightCalcFactory(grabParam);
    }

    @ApiOperation(value = "创建订单")
    @PostMapping(value = "/grabOrder")
    public ResultMessage<Object> grabOrder(@RequestBody GrabOrderParam grabParam) {
        return grabOrderFactory.generateCreateOrderFactory(grabParam);
    }

    @ApiOperation(value = "企叮咚-商品信息修改通知")
    @PostMapping(value = "/goodsCallback")
    public BaseResult goodsCallback(@RequestBody GoodsCallbackRequest goodsCallbackRequest) {
        log.info("商品信息修改通知--" + JSONObject.toJSONString(goodsCallbackRequest));
        GrabNoticeParam grabParam = new GrabNoticeParam();
        grabParam.setGoodsCallbackRequest(goodsCallbackRequest);
        grabParam.setCode(SourceEnums.DINGDONG_NOTICE.name());
        return grabNoticeFactory.goodsCallback(grabParam);
    }

    @ApiOperation(value = "盛创汇联-商品信息修改通知")
    @PostMapping(value = "/goodsXdxtCallback")
    public BaseResult goodsXdxtCallback() {
        log.info("盛创汇联-商品信息修改通知--");
        GrabNoticeParam grabParam = new GrabNoticeParam();
        grabParam.setCode(SourceEnums.XDXT_NOTICE.name());
        return grabNoticeFactory.goodsCallback(grabParam);
    }

    @ApiOperation(value = "企叮咚-订单回调通知--加密数据")
    @PostMapping(value = "/orderCallback")
    public BaseResult orderCallback(@RequestBody OrderCallbackLockRequest orderCallbackLockRequest) throws Exception {
        log.info("订单回调通知修改通知--" + JSONObject.toJSONString(orderCallbackLockRequest));
        //解密内容
        String resonpseLock = RSAUtil.pubDecrypt(orderCallbackLockRequest.getLock(), dingdongConfig.getPublic_key());
        String responseContent = AESUtil.decrypt(orderCallbackLockRequest.getContent(), resonpseLock);
        JSONObject contentJson = JSONObject.parseObject(responseContent);

        OrderCallbackRequest orderCallbackRequest = JSONObject.parseObject(contentJson.getJSONObject("content").getString("data"), OrderCallbackRequest.class);
        GrabNoticeParam grabParam = new GrabNoticeParam();
        grabParam.setOrderCallbackRequest(orderCallbackRequest);
        grabParam.setCode(SourceEnums.DINGDONG_NOTICE.name());
        return grabNoticeFactory.orderCallback(grabParam);
    }

    @ApiOperation(value = "企叮咚-订单回调通知-未加密")
    @PostMapping(value = "/orderCallbackNo")
    public BaseResult orderCallbackNo(@RequestBody OrderCallbackRequest orderCallbackRequest) {
        log.info("订单回调通知修改通知--" + JSONObject.toJSONString(orderCallbackRequest));
        GrabNoticeParam grabParam = new GrabNoticeParam();
        grabParam.setOrderCallbackRequest(orderCallbackRequest);
        grabParam.setCode(SourceEnums.DINGDONG_NOTICE.name());
        return grabNoticeFactory.orderCallback(grabParam);
    }

    @ApiOperation(value = "盛创汇联-订单信息修改通知")
    @PostMapping(value = "/orderXdxtCallback")
    public BaseResult orderXdxtCallback() {
        log.info("盛创汇联-订单信息修改通知--");
        GrabNoticeParam grabParam = new GrabNoticeParam();
        grabParam.setCode(SourceEnums.XDXT_NOTICE.name());
        return grabNoticeFactory.orderCallback(grabParam);
    }
}
